package edu.hawaii.ics.pjdb.partition_function;

import java.util.ArrayList;
import java.util.List;

import edu.hawaii.ics.pjdb.types.Tuple;

public class Modulo implements PartitionFunction {

    @Override
    /**
     * @param arguments arguments[0] = A String representation of an integer denoting the number of partitions to make.
     */
    public ArrayList<ArrayList<Tuple>> partition(List<Tuple> tuples,
            List<String> arguments, Integer partitionFieldIndex) {
        ArrayList<ArrayList<Tuple>> partitions = new ArrayList<ArrayList<Tuple>>();
        int numPartitions = Integer.parseInt(arguments.get(0));

        // Initialize partitions with empty lists
        for (int i = 0; i < numPartitions; i++) {
            partitions.add(new ArrayList<Tuple>());
        }

        // Partition
        int partitionIndex;
        for (Tuple tuple : tuples) {
            partitionIndex = Integer.parseInt(tuple.getEntries().get(partitionFieldIndex));
            partitions.get(partitionIndex % numPartitions).add(tuple);
        }

        return partitions;
    }

}
